﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Common.Logging;
using WuDada.Core.Post;
using WuDada.Core.Post.Service;
using WuDada.Core.Post.Domain;
using WuDada.Provider.ResourceHandle;
using WuDada.Provider.ResourceHandle.Service;
using WuDada.Provider.ResourceHandle.Domain;


public partial class case_in : System.Web.UI.Page
{
    private PostFactory m_PostFactory;
    private IPostService m_PostService;
    private StorageFactory m_StorageFactory;
    private IStorageFileService m_StorageFileService;

    private int m_PostId = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        m_PostFactory = new PostFactory();
        m_PostService = m_PostFactory.GetPostService();
        m_StorageFactory = new StorageFactory();
        m_StorageFileService = m_StorageFactory.GetStorageFileService();

        m_PostId = QueryStringHelper.GetInteger("id", 0);

        if (!IsPostBack)
        {
            LodaDataToUI();
            fillRepeaterPics();
        }
    }

    private void LodaDataToUI()
    {
        PostVO postVO = m_PostService.GetPostById(m_PostId);

        if (postVO != null)
        {
            ltlTitle1.Text = postVO.Title;
            ltlTitle2.Text = postVO.Title;
            ltlImg.Text = GetPic(postVO.PicFileName);
        }
    }

    private string GetPic(string fileName)
    {
        return "<img src='upload/" + fileName + "' width='600' height='364' border='0'>";
    }

    private void fillRepeaterPics()
    {
        //搜尋條件
        string sortField = "SortNo";
        bool sortDesc = false;

        //分頁
        AspNetPager1.RecordCount = m_StorageFileService.CountStorageFileListBySourceId(
            StorageFileVO.StorageSourceType.Post, m_PostId);
        int pageIndex = (AspNetPager1.CurrentPageIndex - 1);
        int pageSize = AspNetPager1.PageSize;

        Repeater1.DataSource = m_StorageFileService.GetStorageFileListBySourceId(
            StorageFileVO.StorageSourceType.Post, m_PostId, pageIndex, pageSize, sortField, sortDesc);
        Repeater1.DataBind();
    }

    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        fillRepeaterPics();
    }

    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemIndex != -1)
        {
            Control ctrl = e.Item;
            StorageFileVO storageFileVO = (StorageFileVO)e.Item.DataItem;
            string pic = string.Format(@"<a title='{1}' rel='lightbox[pics]' href='common/storagefilepichandler.ashx?fileid={0}'>
                <img src='common/storagefilepichandler.ashx?fileid={0}' width='73' height='54' class='Photo-Sslect' />"
            , storageFileVO.StorageFileId, storageFileVO.DisplayName);
            //UIHelper.SetLiteralText(ctrl, "ltlPic", pic);
        }
    }
}